<?php
namespace Base;
class Rest {

    final function getBaseURL(){ return \Util\Config::get('rest_url','Environment');}

    final function get ($resource, $id=false, $req=null) {
        $url = $this->getBaseURL().$resource;
        $url = $id ? $url.'/'.$id :$url;
        $url = $req!=null ? $url.'?'.http_build_query($req) : $url;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $data = curl_exec($curl);

        $body = null;
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            $body = substr($data, $headerSize);
        }
        curl_close($curl);
        return new RestResponse($body);
    }

    final function post ($resource, $req=null, $post=null) {
        $url = $this->getBaseURL().$resource;
        $url = $req!=null ? $url.'?'.http_build_query($req) : $url;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        $data = curl_exec($curl);
        $body = null;
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            //$header = substr($data, 0, $headerSize);
            $body = substr($data, $headerSize);
        }
        curl_close($curl);
        return new RestResponse($body);
    }

    final function put ($resource, $id, $req=null, $post=null) {
        $url = $this->getBaseURL().$resource;
        $url = $id ? $url.'/'.$id :$url;
        $url = $req!=null ? $url.'?'.http_build_query($req) : $url;
        //echo $url;die;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        $data = curl_exec($curl);
//        print_r($data);
        $body = null;
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            //$header = substr($data, 0, $headerSize);
            $body = substr($data, $headerSize);
        }
        curl_close($curl);
        return new RestResponse($body);
    }

    final function delete ($resource, $id, $req=null, $post=null) {
        $url = $this->getBaseURL().$resource;
        $url = $id ? $url.'/'.$id :$url;
        $url = $req!=null ? $url.'?'.http_build_query($req) : $url;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE' );
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        $data = curl_exec($curl);
        $body = null;
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            //$header = substr($data, 0, $headerSize);
            $body = substr($data, $headerSize);
        }

        return new RestResponse($body);
    }
}